<?php
/**
 * Created by PhpStorm.
 * User: zmit
 * Date: 8/5/17
 * Time: 7:56 PM
 */
namespace ApiBundle\Controller\Platform;

use ApiBundle\Controller\BaseController;

use Symfony\Component\HttpFoundation\Request;

class MedalController extends BaseController
{
    /*
     * 获取勋章列表
     */
    public function medalListAction(Request $request)
    {
        $conn = $this->get("database_connection");
        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        $medal_name = $request->get("medal_name");

        $sql = "SELECT * FROM medal WHERE 1";

        //搜索
        if(!empty($medal_name)){
            $sql .= " AND medal_name = '". $medal_name ."' ";
        }


        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";

        $data = $conn->fetchAll($sql);

        return $this->jsonResponse(0,"操作成功",array("info" => $data,"search_all_count"=>$search_all_count,"all_page"=>$all_page));

    }

    /*
     * 编辑/添加勋章
     */
    public function editMedalAction(Request $request)
    {
        if(!$this->isPlatAdminLogged()){
            return $this->plat_response;
        }
        $conn = $this->get("database_connection");
        $id = $request->get("id");
        $medal = $request->get("medal");
        $thumb = $request->get("thumb");

        if(empty($medal) || empty($thumb)){
            return $this->jsonResponse(1,"勋章或图片为空");
        }

        // 编辑时，获取原勋章名称
        $origin_name = $conn->fetchAssoc("SELECT * FROM medal WHERE id = '". $id ."'");
        if($origin_name['medal_name'] != $medal){

            // 判断勋章是否存在
            $is_exit = $conn->fetchAssoc("SELECT * FROM medal WHERE medal_name = '". $medal ."'");

            if($is_exit){
                return $this->jsonResponse(1,"该勋章已存在");
            }
        }


        $data = array(
            "medal_name" => $medal,
            "thumb" => $thumb
        );
        if(empty($id)){

            $data['created_at'] = date("Y-m-d H:i:s");
            $conn->insert("medal",$data);

        } else {
            $where = array("id" => $id);
            $data['updated_at'] = date("Y-m-d H:i:s");
            $conn->update("medal",$data,$where);
        }

        return $this->jsonResponse(0,"操作成功");

    }

    /*
     * 删除勋章
     */
    public function deleteMedalAction(Request $request)
    {
        if(!$this->isPlatAdminLogged()){
            return $this->plat_response;
        }

        $conn = $this->get("database_connection");
        $id = $request->get("id");

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        // 判断该勋章是否被使用
        $company_medal = $conn->fetchAll("SELECT * FROM company_medal WHERE medal_id = '". $id ."'");

        if(count($company_medal) > 0){
            return $this->jsonResponse(1,"该勋章已被使用，不能删除");
        }
        $where = array("id" => $id);

        $conn->delete("medal",$where);
        return $this->jsonResponse(0,"操作成功");
    }

}